# @Time : 2021/8/16 14:12
# @Author : Fioman 
# @Phone : 13149920693

from matplotlib.lines import Line2D
import matplotlib.pyplot as plt
import numpy as np


def get_line_k_and_b(p1, p2):
    """
    湖区某一条直线的斜率和斜距
    :param p1: 点1的坐标
    :param p2: 点2的坐标
    :return:
    """
    if p1[0] == p2[0]:
        # 垂直于x轴,没有k值
        return None, p1[0]
    else:
        k = (p2[1] - p1[1]) / (p2[0] - p1[0])
        b = p1[1] - k * p1[0]
        return k, b


figure, ax = plt.subplots(figsize=(20, 10))

# 设置x和y的值域
ax.set_xlim(left=0, right=1000)
ax.set_ylim(bottom=0, top=1000)

# 两条实现的数据
line1 = [(110.37, 1140.88), (167.44, 63.71)] # 第一边
line2 = [(748.92, 1178.31), (810.08, 86.1)] # 中缝

# line1 = [(75.29, 1215.22), (90.08, 91.96)] # 第一边
# line2 = [ (715.1, 1197.15), (735.91, 112.67)] # 中缝
#### 方式一
# line1 = [(line1[0][0],-line1[0][1]),(line1[1][0],-line1[1][1])]
# line2 = [(line2[0][0],-line2[0][1]),(line2[1][0],-line2[1][1])]

#### 方式二,x和y互相掉位置
line1 = [(line1[0][1],line1[0][0]),(line1[1][1],line1[1][0])]
line2 = [(line2[0][1],line2[0][0]),(line2[1][1],line2[1][0])]
# 然后就是移动两条直线,让其一个点重合,如何去做.就是先计算这条直线的斜率和b

k1, b1 = get_line_k_and_b(line1[0], line1[1])
k2, b2 = get_line_k_and_b(line2[0], line2[1])

k3 = k1  # 第一条线平移.
line3P1 = line2[0]
line3B = line3P1[1] - k3 * line3P1[0]

line3P2X = line2[1][0]

line3P2Y = line3P2X * k3 + line3B

line3 = [line3P1, (line3P2X, line3P2Y)]

k4 = k2
line4P1 = line1[0]
line4B = line4P1[1] - k4 * line4P1[0]
line4P2Y = line1[1][0] * k4 + line4B

line4 = [line4P1, (line1[1][0], line4P2Y)]

(line1Xs, line1Ys) = zip(*line1)
(line2Xs, line2Ys) = zip(*line2)
(line3Xs, line3Ys) = zip(*line3)
(line4Xs, line4Ys) = zip(*line4)

ax.add_line(Line2D(line1Xs, line1Ys, linewidth=1, color="blue"))  # 第一边
ax.add_line(Line2D(line2Xs, line2Ys, linewidth=1, color="red"))  # 中缝

ax.add_line(Line2D(line3Xs, line3Ys, linewidth=1, color="blue"))  # 第一边移动到中缝那里去
ax.add_line(Line2D(line4Xs, line4Ys, linewidth=1, color="red"))  # 中缝移动到第一边那里去

index = np.arange(4)
barWidth = 0.05
opacity = 0.8

rects1 = plt.bar(0.01, 0.5, alpha=opacity, color="b", label="firstEdge")
rects2 = plt.bar(0.01, 0.5 + barWidth, alpha=opacity, color="r", label="middle")

ax.legend(loc=0, labelspacing=1, handlelength=3, fontsize=14, shadow=True)
plt.plot()
plt.show()
